// 封装拦截器
import axios from 'axios'
import Storage from './storage'
import Utils from './utils'
import QS from 'qs'

axios.defaults.baseURL = '/'

axios.interceptors.request.use(
  (config) => {
    config.timeout = 20000 * 10 * 1000
    config.withCredentials = true

    // 处理header中的语言参数
    const lang = Storage.get('lang') || 'zh'
    if (Storage.get('curRouter') !== 'login') {
      config.headers['language'] = lang
    }

    // 处理header中的authorization参数
    const authorization = Storage.get('access_token') || ''
    if (authorization) {
      config.headers['authorization'] = 'Bearer ' + authorization
    }
    if (config.headers['Content-Type'] === undefined) {
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    }
    return config
  },
  (error) => {
    if (Utils.isEmpty(error.info)) {
      Object.assign(error, { info: 'Operate Fail！' })
    }
    return Promise.reject(error)
  }
)

// axios.interceptors.response.use(
//   (response) => {
//     debugger
//     if (Object.is(response.data.code, '200') || Object.is(response.data.code, 200)) {
//       return Object.assign({ success: true }, response)
//     } else if (Object.is(response.data.code, '401') || Object.is(response.data.code, 401)) {
//       Storage.clearAll()
//       window.open('/login', '_self')
//     } else {
//       return Promise.reject(response)
//     }
//   },
//   (error) => {
//     if (Utils.isEmpty(error.info)) {
//       Object.assign(error, { info: 'Operate Fail!' })
//     }
//     return Promise.reject(error)
//   }
// )

const get = (url, params) => {
  // 构造查询参数字符串
  const queryString = new URLSearchParams(params).toString()
  const fullUrl = queryString ? `${url}?${queryString}` : url
  return new Promise((resolve, reject) => {
    axios
      .get(fullUrl)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

const post = (url, params) => {
  return new Promise((resolve, reject) => {
    axios
      .post(url, QS.stringify(params))
      .then((res) => {
        if (Object.is(res.data.code, 200)) {
          resolve(res.data)
        } else {
          if (Utils.isEmpty(res.data.msg)) {
            Object.assign(res.data, { msg: 'Operate Fail!' })
          }
          reject(res.data)
        }
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

const postJson = (url, params) => {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params, {
        headers: {
          'Content-Type': 'application/json',
        },
      })
      .then((res) => {
        resolve(res.data)
        // if (Object.is(res.data.code, 200)) {
        // } else {
        //   if (Utils.isEmpty(res.data.msg)) {
        //     Object.assign(res.data, { msg: 'Operate Fail!' })
        //   }
        //   reject(res.data)
        // }
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

export default {
  axios,
  get,
  post,
  install: (app) => {
    app.config.globalProperties['$get'] = get
    app.config.globalProperties['$post'] = post
    app.config.globalProperties['$postJson'] = postJson
    app.config.globalProperties['$axios'] = axios
  },
}
